export function getFormatTime(t: number, format: string) {
    if (!t) return "";
    const currentDate = new Date(t);
    const year = currentDate.getFullYear();
    const month = ("0" + (currentDate.getMonth() + 1)).slice(-2);
    const day = ("0" + currentDate.getDate()).slice(-2);
    const hours = ("0" + currentDate.getHours()).slice(-2);
    const minutes = ("0" + currentDate.getMinutes()).slice(-2);
    const seconds = ("0" + currentDate.getSeconds()).slice(-2);
    switch (format) {
        case "yyyy":
            return `${year}`;
        case "yyyy-MM":
            return `${year}-${month}`;
        case "yyyy-MM-dd":
            return `${year}-${month}-${day}`;
        case "yyyy-MM-dd hh":
            return `${year}-${month}-${day} ${hours}`;
        case "yyyy-MM-dd hh:mm":
            return `${year}-${month}-${day} ${hours}:${minutes}`;
        case "yyyy-MM-dd hh:mm:ss":
            return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
        case "MM-dd":
            return `${month}-${day}`;
    }
}

export function format(dateString: string) {
    let date = new Date(dateString);
    // date.setHours(date.getHours() + 8);

    const year = date.getFullYear().toString();
    const month = ("0" + (date.getMonth() + 1)).slice(-2);
    const day = ("0" + date.getDate()).slice(-2);
    const hours = ("0" + date.getHours()).slice(-2);
    const minutes = ("0" + date.getMinutes()).slice(-2);
    const seconds = ("0" + date.getSeconds()).slice(-2);

    const formattedDate = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
    return formattedDate;
}
export function formatTimeAgo(timestamp: any) {
    const seconds = Math.floor((Date.now() - timestamp) / 1000);

    if (seconds < 60) {
        return seconds + "秒前";
    }

    const minutes = Math.floor(seconds / 60);
    if (minutes < 60) {
        return minutes + "分钟前";
    }

    const hours = Math.floor(minutes / 60);
    if (hours < 24) {
        return hours + "小时前";
    }

    const days = Math.floor(hours / 24);
    if (days < 30) {
        return days + "天前";
    }

    const months = Math.floor(days / 30);
    if (months < 12) {
        return months + "个月前";
    }

    const years = Math.floor(months / 12);
    return years + "年前";
}

export function formatDuratione(seconds: number) {
    if (!seconds) return 0;
    seconds = Math.floor(seconds);
    const ary = [];
    const hours = Math.floor(seconds / 3600);
    hours && ary.push(hours.toString().padStart(2, "0"));
    const minutes = Math.floor((seconds % 3600) / 60);
    (minutes || hours) && ary.push(minutes.toString().padStart(2, "0"));
    const remainingSeconds = seconds % 60;
    if (ary.length == 0) return "0:" + remainingSeconds;
    ary.push(remainingSeconds.toString().padStart(2, "0"));
    let formattedTime = ary.join(":");
    return formattedTime;
}
